﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace AzureToolkit.Engine
{
    public static class RetryExtensions
    {

        public static TResult RunWithRetries<TResult>(this Func<TResult> func, TimeSpan retryDelay)
        {
            while (true)
            {
                int retries = 0;
                try
                {
                    return func();
                }
                catch
                {
                    if (retries > 3)
                    {
                        throw;
                    }
                    Thread.Sleep(retryDelay);
                }
            }
        }

        public static TResult RunWithRetries<T, TResult>(this Func<T, TResult> func, TimeSpan retryDelay, T arg)
        {
            while (true)
            {
                int retries = 0;
                try
                {
                    return func(arg);
                }
                catch
                {
                    if (retries > 3)
                    {
                        throw;
                    }
                    Thread.Sleep(retryDelay);
                }
            }
        }

        public static TResult RunWithRetries<T1, T2, TResult>(this Func<T1, T2, TResult> func, TimeSpan retryDelay, T1 arg1, T2 arg2)
        {
            while (true)
            {
                int retries = 0;
                try
                {
                    return func(arg1, arg2);
                }
                catch
                {
                    if (retries > 3)
                    {
                        throw;
                    }
                    Thread.Sleep(retryDelay);
                }
            }
        }


    }
}
